package com.hzit.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Component
public class AlipayTemplate {

	@Autowired
	private PayProperties payProperties;

	//1. 生成的支付页面，将当前项目与支付宝对接
	public String createPayPage(String outTradeNo,Long money) throws AlipayApiException {
		//获得初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient(payProperties.getGatewayUrl(),
					payProperties.getApp_id(),
					payProperties.getMerchant_private_key(),
				"json", payProperties.getCharset(),
				    payProperties.getAlipay_public_key(),
				   payProperties.getSign_type());

		//设置请求参数
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
		alipayRequest.setReturnUrl(payProperties.getReturn_url());      // 设置同步回调地址
		alipayRequest.setNotifyUrl(payProperties.getNotify_url());      // 设置异步回调地址

		//订单名称，必填
		String subject = "泽易购订单";
		//商品描述，可空
		String body = "购买的都是电脑耗材";

		alipayRequest.setBizContent("{\"out_trade_no\":\""+ outTradeNo +"\","
				+ "\"total_amount\":\""+ money +"\","
				+ "\"subject\":\""+ subject +"\","
				+ "\"body\":\""+ body +"\","
				+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

		//若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
		//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
		//		+ "\"total_amount\":\""+ total_amount +"\","
		//		+ "\"subject\":\""+ subject +"\","
		//		+ "\"body\":\""+ body +"\","
		//		+ "\"timeout_express\":\"10m\","
		//		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
		//请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

		// 请求并生成产生页面的字符串
		String result = alipayClient.pageExecute(alipayRequest).getBody();
		// 返回给控制器方法
		return result;
	}
}
